package component.auxiliares;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.JTextField;

import component.auxiliares.pojo.SliderPojo;
import component.auxiliares.view.SliderConfig;
import component.util.BaseMotionAdapter;

public class Slider extends JSlider {
	private JPopupMenu popupComponent = new JPopupMenu();
	private JMenuItem menuComponentRename = new JMenuItem("Renomear...");
	private JMenuItem menuComponentExclude = new JMenuItem("Excluir");
	private JMenuItem menuComponentProperties = new JMenuItem("Propriedades...");

	private SliderPojo pojo;
	private JPanel panel;

	public Slider(JPanel panel, MouseEvent evt) {

		this.pojo = new SliderPojo();

		this.setBounds(evt.getX(), evt.getY(), 60, 20);
		this.setEnabled(false);
		this.setSize(110, 20);
		panel.add(this);
		this.panel = panel;
		new BaseMotionAdapter(this, panel);
		pojo.setX(evt.getX());
		pojo.setY(evt.getY());

		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				txtFieldMouseClicked(evt);
			}
		});

		menuComponentRename.setEnabled(false);
		menuComponentExclude.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentExcludeActionPerformed();
			}
		});
		menuComponentProperties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentPropertiesActionPerformed();
			}
		});
		popupComponent.add(menuComponentRename);
		popupComponent.add(menuComponentExclude);
		popupComponent.add(menuComponentProperties);
	}

	private void menuComponentExcludeActionPerformed() {
		String[] options = { "Ok", "Cancelar" };
		int result = JOptionPane.showOptionDialog(null, "Você tem certeza?",
				"Excluir", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
		if (result == 0) {
			try {
				panel.remove(this);
				panel.repaint();
				this.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	private void menuComponentPropertiesActionPerformed() {
		this.montaJanela();
	}

	public void setPojoConfig(String texto, Color color, JComboBox cmbTamanho,
			JComboBox cmbFonte) {
		// TODO
	}

	private void txtFieldMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2) {
			this.montaJanela();
		} else if (evt.getButton() == evt.BUTTON3) {
			popupComponent.show(this, evt.getX(), evt.getY());
		}
	}

	public void atualiza(SliderConfig frame) {
		pojo.setMax(Float.parseFloat(frame.getTxtMax().getText()));
		pojo.setMin(Float.parseFloat(frame.getTxtMin().getText()));
		pojo.setReference(Float.parseFloat(frame.getTxtRef().getText()));
		// pojo.setIdVariable(frame.getCmbVariable().getSelectedIndex());
		// pojo.setIdVariable(idVariable);// TODO SETAR O ID DA VARIAVEL
		frame.dispose();
	}

	private void montaJanela() {
		SliderConfig frame = new SliderConfig(this);
		JTextField txtMin = frame.getTxtMin();
		JTextField txtMax = frame.getTxtMax();
		JTextField txtRef = frame.getTxtRef();
		JComboBox cmbVar = frame.getCmbVariable();

		txtMin.setText(String.valueOf(pojo.getMin()));
		txtMax.setText(String.valueOf(pojo.getMax()));
		txtRef.setText(String.valueOf(pojo.getReference()));
		// try {
		// cmbVar.setSelectedIndex(pojo.getIdVariable());
		// } catch (Exception e) {
		// cmbVar.setSelectedIndex(0);
		// }
		frame.setVisible(true);
	}

	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		pojo.setX(x);
		pojo.setY(y);
	}

}
